package com.spring.cloud.exception;

import com.spring.cloud.enums.BusinessMessage;

/**
 * 业务异常类
 *
 * @author suguolin
 */
public class BusinessException extends RuntimeException{

    private BusinessMessage businessMessage;

    public static BusinessException of(BusinessMessage businessMessage) {
        return new BusinessException(businessMessage);
    }

    /**
     * 可以添加额外信息的运行时异常方法
     * @param businessMessage
     * @param otherMsg 加额外信息
     */
    public static BusinessException of(BusinessMessage businessMessage,String otherMsg) {
        String msg = businessMessage.getMsg();
        Integer code = businessMessage.getCode();
        return new BusinessException(new BusinessMessage() {
            @Override
            public Integer getCode() {
                return code;
            }
            @Override
            public String getMsg() {
                return msg+","+otherMsg;
            }
        });
    }

    private BusinessException(BusinessMessage businessMessage) {
        this.businessMessage = businessMessage;
    }

    public BusinessMessage getBusinessMessage() {
        return businessMessage;
    }

    public void setBusinessMessage(BusinessMessage businessMessage) {
        this.businessMessage = businessMessage;
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }
}
